package com.courseware.online.common;

import com.courseware.online.controller.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author demos
 */
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {

    @ExceptionHandler({Exception.class})
    @ResponseBody
    public ResultVO handleIndexOutOfBoundsException(Exception e) {
        ResultVO resultVO;
        if (e instanceof BusinessException) {
            BusinessException businessException = (BusinessException) e;
            log.error(businessException.getMessage(), businessException);
            resultVO = new ResultVO(businessException.getCode(),businessException.getMessage());
            return resultVO;
        }
        log.error("未知异常", e);
        return new ResultVO(0, "服务器异常");
    }
}
